package com.mbg17.utils;

import com.alibaba.fastjson.JSONObject;
import com.mbg17.domain.ApiTestInputDto;
import com.mbg17.domain.Datas;
import io.restassured.internal.RequestSpecificationImpl;
import io.restassured.internal.TestSpecificationImpl;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Locale;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.request;

/**
 * @author LuYuan
 * @version 1.0
 * @date 2022/1/20 8:41 下午
 * @File : RestAssuredUtils.java
 * @Describe: IntelliJ IDEA
 */
@Component
public class RestAssuredUtils {

    public static RequestSpecification requestEncapsulates(ApiTestInputDto apiTestInputDto) {
        RequestSpecification given = given();
        for (Datas header : apiTestInputDto.getHeaders()) {
            given.header(header.getKey(), header.getValue());
        }
        for (Datas param : apiTestInputDto.getParams()) {
            given.queryParams(param.getKey(), param.getValue());
        }
        for (Datas urlencoded : apiTestInputDto.getUrlencodeds()) {
            given.formParams(urlencoded.getKey(), urlencoded.getValue());
        }
        if (apiTestInputDto.getJson()!=null && apiTestInputDto.getJson().length() > 2) {
            given.body(apiTestInputDto.getJson());
        }
        return given;
    }

    public static Response POST(String url,RequestSpecification given) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        return given.post(url).then().extract().response();
    }

    public static Response GET(String url,RequestSpecification given) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        return given.get(url).then().extract().response();
    }

    public static Response DELETE(String url,RequestSpecification given) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        return given.delete(url).then().extract().response();
    }

    public static Response PUT(String url,RequestSpecification given) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        return given.put(url).then().extract().response();
    }

    public static Response PATCH(String url,RequestSpecification given) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        return given.patch(url).then().extract().response();
    }
}
